﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using DAL.Help;
using System.Data;
using DAL.Connect;

namespace DAL.Classes
{
    public class ThongTinBaiDang
    {
        #region khai bao cac thuoc tinh anh xa
        public int MaThongTinBaiDang { get; set; }
        public int MaBaiDang { get; set; }
        public string HinhAnh { get; set; }
        public string MoTa { get; set; }
        public ThongTinBaiDang() { }
        #endregion

        #region Cac phuong thuc them xoa sua
        public static ThongTinBaiDang LayTatCa()
        {
            try
            {
                return CBO.FillObject<ThongTinBaiDang>(DataProvider.Instance.ExecuteReader("ThongTinBaiDang_LayTatCa"));
            }
            catch
            {
                return null;
            }
        }
        public static bool Them(ThongTinBaiDang TTBD)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@MaThongTinBaiDang", "ThongTinBaiDang_Them",
                    TTBD.MaThongTinBaiDang, TTBD.MaBaiDang, TTBD.HinhAnh, TTBD.MoTa);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Sua(ThongTinBaiDang TTBD)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("ThongTinBaiDang_Sua",
                    TTBD.MaThongTinBaiDang, TTBD.MoTa);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Xoa(string maThongTinBaiDang)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("ThongTinBaiDang_Xoa", Convert.ToInt32(maThongTinBaiDang));
                return rs > 0;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Cac phuong thuc lay du lieu
        public static ThongTinBaiDang ThongTinBaiDang_LayTheoMa(string maThongTinBaiDang)
        {
            try
            {
                return CBO.FillObject<ThongTinBaiDang>(DataProvider.Instance.ExecuteReader("ThongTinBaiDang_LayTheoMa", maThongTinBaiDang));
            }
            catch
            {
                return null;
            }
        }
        public static List<ThongTinBaiDang> ThongTinBaiDang_LayTheoMaBaiDang(string maBaiDang)
        {
            try
            {
                return CBO.FillCollection<ThongTinBaiDang>(DataProvider.Instance.ExecuteReader("ThongTinBaiDang_LayTheoMaBaiDang", ConvertType.ToInt32(maBaiDang)));
            }
            catch
            {
                return null;
            }
        }
        public static List<ThongTinBaiDang> LayThongTinBaiDangTheoMaBaiDang_PT(string maBaiDang, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = 12;
                reader = DataProvider.Instance.ExecuteReader("ThongTinBaiDang_LayTheoMaBaiDang_PT", Convert.ToInt32(maBaiDang),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<ThongTinBaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0; return new List<ThongTinBaiDang>();
            }
        }
        #endregion
    }
}
